home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / superv22.zip / EXEC.PAS < prev    next >
Pascal/Delphi Source File  |  1986-12-02  |  5KB  |  164 lines

  1. Type
  2.   Exec_Str66=String[66];    {Used for paths, filenames, etc}
  3.   Exec_Str255=String[255];  {Main command string type}
  4.  
  5. Var
  6.   Exec_Regs: Record Case Integer Of
  7.                 1: (AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: Integer);
  8.                 2: (AL,AH,BL,BH,CL,CH,DL,DH: Byte);
  9.               End;
  10.   { NOTE: the above variable is referenced in an Inline statement.  It MUST
  11.     be a global variable (not a local variable or a typed constant)!! Ignore
  12.     this and you'll find yourself in Wonderland faster than you can reach
  13.     for the off switch}
  14.  
  15. Function GetEnvStr(SearchString: Exec_Str255): Exec_Str255;
  16.   Type
  17.     Env=Array [0..32767] Of Char;
  18.   Var
  19.     EPtr: ^Env;
  20.     EStr: Exec_Str255;
  21.     Done: Boolean;
  22.     I: Integer;
  23.  
  24.   Begin
  25.     GetEnvStr:='';
  26.     If SearchString<>'' Then
  27.      Begin
  28.       EPtr:=Ptr(MemW[CSeg:$002C],0);
  29.       I:=0;
  30.       SearchString:=SearchString+'=';
  31.       Done:=False;
  32.       EStr:='';
  33.       Repeat
  34.         If EPtr^[I]=#0 Then
  35.          Begin
  36.           If EPtr^[Succ(I)]=#0 Then
  37.            Begin
  38.             Done:=True;
  39.             If SearchString='==' Then
  40.              Begin
  41.               EStr:='';
  42.               I:=I+4;
  43.               While EPtr^[I]<>#0 Do
  44.                Begin
  45.                 EStr:=EStr+EPtr^[I];
  46.                 I:=Succ(I);
  47.                End;
  48.               GetEnvStr:=EStr;
  49.              End;
  50.            End;
  51.           If Copy(EStr,1,Length(SearchString))=SearchString Then
  52.            Begin
  53.             GetEnvStr:=Copy(EStr,Succ(Length(SearchString)),255);
  54.             Done:=True;
  55.            End;
  56.           EStr:='';
  57.          End
  58.         Else EStr:=EStr+EPtr^[I];
  59.         I:=Succ(I);
  60.       Until Done;
  61.      End;
  62.   End;
  63.  
  64. Function ComSpec: Exec_Str66;
  65.   Begin
  66.     ComSpec:=GetEnvStr('COMSPEC');
  67.   End;
  68.  
  69. Function Exec(CommandLine: Exec_Str255): Integer;
  70.   Const
  71.     SSSave: Integer=0;
  72.     SPSave: Integer=0;
  73.  
  74.   Var
  75.     FCB1,FCB2: Array [0..36] Of Byte; {*}
  76.     PathName: Exec_Str66;            {*}
  77.     CommandTail: Exec_Str255;
  78.     ParmTable: Record                 {*}
  79.                  EnvSeg: Integer;
  80.                  ComLin: ^Integer;
  81.                  FCB1Pr: ^Integer;
  82.                  FCB2Pr: ^Integer;
  83.                End;
  84.     RegsFlags: Integer;               {*}
  85.     {*: these variables are accessed in an Inline statement; their
  86.         declarations must not be changed }
  87.  
  88.   Begin
  89.   (* Process the command, making sure that its in proper format *)
  90.     If Pos(' ',CommandLine)=0 Then
  91.      Begin
  92.       PathName:=CommandLine+#0;
  93.       CommandTail:=^M;
  94.      End
  95.     Else
  96.      Begin
  97.       PathName:=Copy(CommandLine,1,Pred(Pos(' ',CommandLine)))+#0;
  98.       CommandTail:=Copy(CommandLine,Pos(' ',CommandLine),255)+^M;
  99.      End;
  100.     CommandTail[0]:=Pred(CommandTail[0]);
  101.     (* Now set up the Function 4B data block *)
  102.     With Exec_Regs Do
  103.      Begin
  104.       FillChar(FCB1,Sizeof(FCB1),0);
  105.       AX:=$2901;
  106.       DS:=Seg(CommandTail[1]);
  107.       SI:=Ofs(CommandTail[1]);
  108.       ES:=Seg(FCB1);
  109.       DI:=Ofs(FCB1);
  110.       MsDos(Exec_Regs); { Create FCB 1 }
  111.       FillChar(FCB2,Sizeof(FCB2),0);
  112.       AX:=$2901;
  113.       ES:=Seg(FCB2);
  114.       DI:=Ofs(FCB2);
  115.       MsDos(Exec_Regs); { Create FCB 2 }
  116.       With ParmTable Do
  117.        Begin
  118.         EnvSeg:=MemW[CSeg:$002C];     { Makes sure valid environ is passed }
  119.         ComLin:=Addr(CommandTail);
  120.         FCB1Pr:=Addr(FCB1);
  121.         FCB2Pr:=Addr(FCB2);
  122.        End;
  123.       (* Now this inline does the actual work. It could be done without
  124.          inline code, but with so much stack manipulation I felt more
  125.          comfortable doing it in this manner. Not that no allocation of
  126.          memory is necessary since Turbo automatically does that. (Isn't
  127.          that nice of them!) *)
  128.  
  129.       InLine($8D/$96/ PathName /$42/  { <DX>:=Ofs(PathName[1]); }
  130.              $8D/$9E/ ParmTable /     { <BX>:=Ofs(ParmTable);   }
  131.              $B8/$00/$4B/             { <AX>:=$4B00;            }
  132.              $1E/$55/                 { Save <DS>, <BP>         }
  133.              $16/$1F/                 { <DS>:=Seg(PathName[1]); }
  134.              $16/$07/                 { <ES>:=Seg(ParmTable);   }
  135.              $2E/$8C/$16/ SSSave /    { Save <SS> in SSSave     }
  136.              $2E/$89/$26/ SPSave /    { Save <SP> in SPSave     }
  137.              $FA/                     { Disable interrupts      }
  138.              $CD/$21/                 { Call PC-DOS             }
  139.              $FA/                     { Disable interrupts      }
  140.              $2E/$8B/$26/ SPSave /    { Restore <SP>            }
  141.              $2E/$8E/$16/ SSSave /    { Restore <SS>            }
  142.              $FB/                     { Enable interrupts       }
  143.              $5D/$1F/                 { Restore <BP>,<DS>       }
  144.              $9C/$8F/$86/ RegsFlags / { Flags:=<CPU flags>      }
  145.              $A3/ Exec_Regs );        { Exec_Regs.AX:=<AX>;    }
  146.  
  147.       If (RegsFlags And 1)<>0 Then Exec:=AX
  148.       Else Exec:=0;
  149.      End;
  150.   End;
  151.  
  152. Function ExecCommand(CommandLine: Exec_Str255): Integer;
  153.   Begin
  154.     ExecCommand := Exec(ComSpec +' /C '+CommandLine);
  155.   End;
  156.  
  157. Function Shell: Integer;
  158.   Begin
  159.     Shell:=Exec(ComSpec);
  160.   End;
  161.  
  162.  
  163.  
  164.